// Font/BMFont.h

#ifndef _FONT_BMFont_H_
#define _FONT_BMFont_H_

#ifndef _CORE_std_H_
#include <core/std.h>
#endif

#ifndef _FONT_FontTypes_H_
#include <Font/FontTypes.h>
#endif

#include <helper/Markup.h>

namespace GFX
{
	class Texture;
};

namespace FONT
{

class BMFont
{
public:

	enum BMFormatType
	{
		kBinary = 0,
		kText,
		kXML,
		kTotal
	};

	//
	// Lifecycle
	//
							BMFont();
							~BMFont();

	// Accessor
	void					SetTexture(int iIndex, GFX::Texture *pTexture) {
		m_pTextures[iIndex] = pTexture;
	}

	const GFX::Texture *	GetTexture(int iTexture) const {
		return m_pTextures[iTexture];
	}
	int						GetTextureCount() const {
		return m_iNumTextures;
	}
	const char *			GetTextureName(int iIndex) const {
		return m_pszTextureNames[iIndex];
	}

	const FONT::commonBlock	*GetCommonBlock() const {
		return m_pCommonBlock;
	}

	const charInfo *		FindCharInfo( wchar_t ch ) const;
	const kerningPair *		FindKerningOffset( const wchar_t & First, const wchar_t & Second ) const;

	//
	// API
	//

	void					GetTextDimensions( const wchar_t * pText, float32_t & fWidth, 
											   float32_t & fHeight, float32_t fHScale = 1.0f, float32_t fVScale = 1.0f ) const;


	// Load font information from the binary data block and find the texture names.
	// The BMFont then holds the pointer to this data and will delete it on destruction.
	void					SetupBMFont( byte * pDataBuffer, int iSize );
	void					SetupXMLBMFont( const char * pszFileName );
	void					SetupTextBMFont( const char * pszFileName );

	// text format loader API's
	int						SkipWhiteSpace(std::string &str, int start);
	int						FindEndOfToken(std::string &str, int start);

	void					InterpretInfo(std::string &str, int start);
	void					InterpretCommon(std::string &str, int start);
	void					InterpertChars(std::string &str, int start);
	void					InterpretChar(std::string &str, int start);
	void					InterpretSpacing(std::string &str, int start);
	void					InterpretKernings(std::string &str, int start);
	void					InterpretKerning(std::string &str, int start);
	void					InterpretPage(std::string &str, int start);


protected:

	unsigned char*			m_pRawData;

	infoBlock*				m_pInfoBlock;
	commonBlock*			m_pCommonBlock;
	charInfo*				m_pCharInfo;
	kerningPair*			m_pkerningPair;
	int						m_CharCount;
	int						m_kerningCount;

	const char *			m_pszTextureNames[FONT_MAX_TEXTUREPAGES];
	std::string				m_strTextureNames[FONT_MAX_TEXTUREPAGES];
	int						m_iNumTextures;

	GFX::Texture *			m_pTextures[FONT_MAX_TEXTUREPAGES];

	BMFormatType			m_FormatType;
};

}; // namespace BMFont

#endif